// Created by WXX on 2022/1/28 21:23
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：628 ms, 在所有 C++ 提交中击败了42.03%的用户
 * 内存消耗：122.2 MB, 在所有 C++ 提交中击败了65.22%的用户
 */
class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& ps) {

        sort(ps.begin(), ps.end());

        int n = ps.size();
        int res = 0, a = ps[n - 1][1];
        for (int i = n - 2; i >= 0; i--) {
            if (ps[i][0] != ps[i + 1][0]) {
                int b = 0;
                int j = i;
                while (j >= 0 && ps[j][0] == ps[i][0]) {
                    if (a > ps[j][1]) res++;
                    b = max(b, ps[j][1]);
                    j--;
                }
                i = j + 1;
                a = max(a, b);
            } else {
                a = max(a, ps[i][1]);
            }
        }

        return res;
    }
};

int main() {

    vector<vector<int>> ps = {
            {5, 5},
            {6, 3},
            {3, 6}
    };
    cout << Solution().numberOfWeakCharacters(ps) << endl;  // 0

    return 0;
}
